package com.dhcc.wb.articleday;

import java.util.List;
import java.util.UUID;

import org.json.JSONObject;

import com.dhcc.common.util.StringUtil;
import com.dhcc.wb.gongzhonghao.GzhDao;
import com.dhcc.wb.gongzhonghao.GzhModel;
import com.dhcc.wb.util.HttpUtil;
import com.dhcc.wb.util.TokenUtil;
import com.google.gson.Gson;

/**
 * @author wb
 *
 */
public class ArticleTask{
	/**
	 * 获取图文群发每日数据
	 */
	private static String getarticlesummaryUrl = "https://api.weixin.qq.com/datacube/getarticlesummary?access_token=";//获取图文群发每日数据


	
	/**
	 * 循环所有公众号
	 */
	public static void updateArticle(String begin_date,String end_date){
		GzhDao gzhdao = new GzhDao();
		List<GzhModel> gzhlist = gzhdao.getAll();
		if(gzhlist != null && gzhlist.size()>0){
			for(GzhModel gzh :gzhlist){
				updateSingleArticle(gzh.getGzhid(),begin_date,end_date,gzh.getGzhname());
			}
		}
	}
	
	
	/**
	 * 新增一个公众号文章
	 * begin_date end_date 最大时间跨度 1
	 * 获取的是 这个时间段阅读的文章 当天的阅读量
	 * 
	 * begin end 阅读的文章 在当天的统计 阅读量
	 */
	public static void updateSingleArticle(String gzhid,String begin_date,String end_date,String gzhname){
		try {
			String token = TokenUtil.getToken(gzhid);
			if(!StringUtil.isNullOrEmpty(token)){
				JSONObject json = new JSONObject();
				json.put("begin_date", begin_date);
				json.put("end_date", end_date);
				String content1 = HttpUtil.postContent(getarticlesummaryUrl+token, json.toString());
				
				Gson gson2 = new Gson();
				ArticleListModel articleList = gson2.fromJson(content1, ArticleListModel.class);
				
				if(articleList != null && articleList.getList() != null && articleList.getList().size()>0){
					for(ArticleModel ent:articleList.getList()){
						ArticledayDao dao = new ArticledayDao();
						ArticleModel art = dao.queryArticle_result(ent.getTitle(), ent.getMsgid(),ent.getRef_date(),gzhid);
						if(art == null){
							dao.addArticle(UUID.randomUUID().toString(), ent.getRef_date(), ent.getMsgid(), ent.getTitle(), ent.getInt_page_read_user(), ent.getInt_page_read_count(), ent.getOri_page_read_user(), ent.getOri_page_read_count(), ent.getShare_user(), ent.getShare_count(), ent.getAdd_to_fav_user(), ent.getAdd_to_fav_count(), gzhid,gzhname);
						}
						
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
	
	

	

    public static void main(String[] args) {
//    	updateSingleArticle("00bd81a7-d6af-42dd-b1c6-637fbf4159cf","2019-11-16","2019-11-16", "酷航科技【测试】");
//    	updateArticle("2019-11-16","2019-11-16");
    	try {
    		Gson gson2 = new Gson();
        	ArticleListModel articleList = gson2.fromJson("ssssssssssss", ArticleListModel.class);
        	System.out.println(articleList);
		} catch (Exception e) {
			// TODO: handle exception
		}
    	
    	
    	
	}
		
		
	
}
